// pages/scrollTest/test.js
let utils = require('../../utils/util.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    wxid:'test0001',
    isShow: true,
    scrollHeight: 480,
    tabs: ["我的段位", "好友排名", "世界排名"],
    activeIndex: 0,

    myData: {},
    friendPage: 1,
    worldPage: 1,
    friendList: [],
    worldList: [],
    isLastPageF: false,
    isLastPageW: false
  },
  tabClick: function (e) {
    let that = this;
    wx.setNavigationBarTitle({
      title: that.data.tabs[e.currentTarget.id],
    });
    this.setData({
      activeIndex: e.currentTarget.id
    });
  },
  loadworld: function (){
    let that = this;
    if (that.data.isLastPageW) {
      return false;
    }
    wx.request({
      url: `https://tydr.tihe-china.com/rank/global`, //仅为示例，并非真实的接口地址
      // url: `http://120.27.224.239:8888/rank/global`, //仅为示例，并非真实的接口地址
      method: 'GET',
      data: {
        page: that.data.worldPage
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          worldList: [
            ...that.data.worldList,
            ...res.data
          ],
          worldPage: that.data.worldPage + 1,
          isLastPageW: res.data.length<15? true :false
        });
      }
    });
  },
  loadfriend: function (){
    let that = this;
    if (that.data.isLastPageF){
      return false;
    }
    wx.request({
      url: `https://tydr.tihe-china.com/rank/friend/${that.data.wxid}`, //仅为示例，并非真实的接口地址
      // url: `http://120.27.224.239:8888/rank/friend/${that.data.wxid}`, //仅为示例，并非真实的接口地址
      method: 'GET',
      data: {
        page: that.data.friendPage
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          friendList: [
            ...that.data.friendList,
            ...res.data
          ],
          friendPage: that.data.friendPage + 1,
          isLastPageF: res.data.length < 15 ? true : false
        });
      }
    });
  },
  loadMy: function() {
    let that = this;
    wx.request({
      url: `https://tydr.tihe-china.com/user/${that.data.wxid}`, //仅为示例，并非真实的接口地址
      // url: `http://120.27.224.239:8888/user/${that.data.wxid}`, //仅为示例，并非真实的接口地址
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          myData: res.data
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    wx.setNavigationBarTitle({
      title: '我的段位',
    })

    this.setData({
      userInfo: app.globalData.userInfo
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight - 80
        });
      }
    })

    wx.getStorage({
      key: 'wx_id',
      success: function (res) {

        that.setData({
          wxid: res.data
        }, function(){
          that.loadMy();
          that.loadfriend();
          that.loadworld();
        });
      }
    });

    
    
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '太影达人',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})